﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;

namespace FastDeal.Admin
{
    public partial class TimKiem : System.Web.UI.Page
    {
        TaiKhoan tk = new TaiKhoan();
        List<TaiKhoan> listtk = new List<TaiKhoan>();
        TaiKhoan_BUS db = new TaiKhoan_BUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 1)
            {
                Response.Redirect("~/Default.aspx");
            }
            if (!IsPostBack)
            {

                LoadDanhSach();
            }
        }
        private void TimKiem1()
        {
            listtk = db.Tim_UserName(txtTimKiem.Text);
            gvTimKiem.DataSource = listtk;
            gvTimKiem.DataBind();
        }
        private void LoadDanhSach()
        {
            listtk = db.LayDanhSach_TaiKhoan();
            gvTimKiem.DataSource = listtk;
            gvTimKiem.DataBind();
            gvTimKiem.Rows[0].Visible = false;
            int i = 0;
            foreach (TaiKhoan tk1 in listtk)
            {
                if (tk1.TrangThai == 0)
                {
                    ((Label)gvTimKiem.Rows[i].Cells[3].FindControl("lbTinhTrang")).Text = "Banned";
                }
                else
                {
                    ((Label)gvTimKiem.Rows[i].Cells[3].FindControl("lbTinhTrang")).Text = "Active";
                }
                if (tk1.isDelete == 1)
                {
                    gvTimKiem.Rows[i].Visible = false;
                }
                i++;
            }
        }
        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            TimKiem1();
        }

        protected void gvTimKiem_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string userN = ((Label)gvTimKiem.Rows[e.RowIndex].Cells[0].FindControl("lbUserName")).Text;
            //string userN = (gvTimKiem.Rows[e.RowIndex].Cells[0]).Text;
            tk.UserName = userN;
            db.Xoa_TaiKhoan(tk);
            LoadDanhSach();
        }
    }
}